package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.ExpertBean;
import com.shqcjd.yunweiyanoperator.model.pojo.FollowListBean;
import com.shqcjd.yunweiyanoperator.ui.activity.staff.ContactsDetailsActivity;
import com.shqcjd.yunweiyanoperator.ui.adapter.BusinessExpertAdapter;
import com.shqcjd.yunweiyanoperator.ui.adapter.FollowListAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.Config;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.WrapContentLinearLayoutManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 我的关注
 */
public class FollowListActivity extends BaseActivity{
    private static final int PAGE_SIZE = Config.PAGE_TOTAL;
    private int mNextRequestPage = 1;

    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_expert_list)
    RecyclerView rvExpertList;
    @BindView(R.id.sr_expert)
    SwipeRefreshLayout swipeLayout;
    private View notDataView;
    private FollowListAdapter followListAdapter;
    private Context mContext;
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_follow_list;
    }

    @Override
    public void initView() {
        tvTitle.setText("我的关注");
        mContext = this;
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvExpertList.getParent(), false);
        initRefreshLayout();
        rvExpertList.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
        followListAdapter = new FollowListAdapter();
        rvExpertList.setAdapter(followListAdapter);
        followListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FollowListBean item = (FollowListBean) adapter.getItem(position);
                Intent intent = new Intent(mContext, ContactsDetailsActivity.class);
                intent.putExtra("isAddFriends", true);
                intent.putExtra("remoteId", item.id);
                intent.putExtra("friendId", item.id);
                intent.putExtra("userName", item.userName);
                intent.putExtra("phoneNumber", item.phoneNumber);
                intent.putExtra("businessName", item.businessName);
                intent.putExtra("maintainInfo", item.info);
                intent.putExtra("acceptAppCode", item.appCode);
                startActivity(intent);
            }
        });

    }


    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initData() {
        refresh();
    }

    private void initRefreshLayout() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        swipeLayout.setRefreshing(true);
    }

//    @Override
//    public void onLoadMoreRequested() {
//        loadMore();
//    }

    private void refresh() {
        mNextRequestPage = 1;
        followListAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        ApiService.getFollowList()
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<FollowListBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        swipeLayout.setRefreshing(true);
                    }
                    @Override
                    public void _onNext(ResponseData<List<FollowListBean>> retData) {
                        if (retData.retCode == 10000) {
                            setData(true, retData.data,followListAdapter);
                        } else {
                            ToastUtils.showShort(mContext,retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext,errorMessage);
                        followListAdapter.setEnableLoadMore(true);
                        swipeLayout.setRefreshing(false);
                    }
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        followListAdapter.setEnableLoadMore(true);
                        swipeLayout.setRefreshing(false);
                    }
                });
//        ApiService.getBusinessExpert(new JSONObject(map).toString())
//                .compose(RxSchedulersHelper.io_main())
//                .subscribe(new RxObserver<ResponseData<ExpertBean>>() {
//                    @Override
//                    public void _onSubscribe(Disposable d) {
//                        swipeLayout.setRefreshing(true);
//                    }
//
//                    @Override
//                    public void _onNext(ResponseData<ExpertBean> retData) {
//                        if (retData.retCode == 10000) {
//                            setData(true, retData.data.list,businessExpertAdapter);
//                        } else {
//                            ToastUtils.showShort(mContext,retData.retMsg);
//                        }
//                    }
//
//                    @Override
//                    public void _onError(String errorMessage) {
//                        ToastUtils.showShort(mContext,errorMessage);
//                        businessExpertAdapter.setEnableLoadMore(true);
//                        swipeLayout.setRefreshing(false);
//                    }
//                    @Override
//                    public void onComplete() {
//                        super.onComplete();
//                        businessExpertAdapter.setEnableLoadMore(true);
//                        swipeLayout.setRefreshing(false);
//                    }
//                });
    }

//    private void loadMore() {
//        Map<String, String> map = new HashMap<>();
//        map.put("keyword", keyword);
//        map.put("page", mNextRequestPage+"");
//        map.put("size", PAGE_SIZE+"");
//        ApiService.getBusinessExpert(new JSONObject(map).toString())
//                .compose(RxSchedulersHelper.io_main())
//                .subscribe(new RxObserver<ResponseData<ExpertBean>>() {
//                    @Override
//                    public void _onNext(ResponseData<ExpertBean> retData) {
//                        if (retData != null && retData.retCode == 10000) {
//                            boolean isRefresh =mNextRequestPage ==1;
//                            setData(isRefresh, retData.data.list,businessExpertAdapter);
//                        } else {
//                            ToastUtils.showShort(mContext, retData.retMsg);
//                        }
//                    }
//
//                    @Override
//                    public void _onError(String errorMessage) {
//                        ToastUtils.showShort(mContext, errorMessage);
//                        businessExpertAdapter.loadMoreFail();
//                    }
//                });
//    }

    private void setData(boolean isRefresh, List data, BaseQuickAdapter adapter) {

        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (size > 0) {
                adapter.setNewData(data);
            } else {
                adapter.setNewData(null);
                adapter.setEmptyView(notDataView);
            }
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }
}
